<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\StudentCourse;
use App\Models\Teacher;
use Illuminate\Http\Request;

/**
 * Class StudentCoursesController
 * @package App\Http\Controllers\Admin
 * User: ZengMingYong
 * Date: 2020/7/1
 * Time: 7:19
 * 课程表
 */
class StudentCourseController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     */
    public function index()
    {
        //获取全部数据
        $data= $data=StudentCourse::orderBy('id','desc')->paginate($this->page);

        return view('admin.student_course.index',compact('data'));
    }

    public function create()
    {

        return view('admin.student_course.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     */
    public function store(Request $request)
    {
        if ($request->ajax()){ //sjax验证老师是否存，返回老师id
            try {
                $post=$this->validate($request,[
                   'teacher'=>'required'
                ]);
                $teacher=Teacher::where('truename',$request->get('teacher'))->withTrashed()->first();
                if ($teacher==null){
                    return ['status'=>3,'msg'=>'验证不通过'];
                }
                return ['status'=>0,'teacher'=>$teacher->id,'msg'=>'验证通过'];
            }catch (\Exception $e){
                return ['status'=>2,'teacher'=>1,'msg'=>'验证不通过'];
            }
        }
        //post添加
        $post=$this->validate($request,[
           'classes'=>'required',
            'schooltime'=>'required',
            'week'=>'required',
            'subject'=>'required',
            'teachers_id'=>'required',
        ]);
        StudentCourse::create($post);
        return redirect(route('admin.student_course.index'))->with(['error'=>'添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\StudentCourse  $studentCourses
     * @return \Illuminate\Http\Response
     */
    public function show(StudentCourse $studentCourses)
    {
        //
    }

    /**
     *
     */
    public function edit(StudentCourse $studentCourse)
    {
      return view('admin.student_course.edit',compact('studentCourse'));
    }

    /**
     * @param Request $request
     * @param StudentCourse $studentCourse
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(Request $request, StudentCourse $studentCourse)
    {

        $this->validate($request,[
            'classes'=>'required',
            'schooltime'=>'required',
            'week'=>'required',
            'subject'=>'required',
            'teacher'=>'required',
        ]);
        $teacher=Teacher::where('truename',$request->input('teacher'))->withTrashed()->first();
        if ($teacher==null){
           return redirect(route('admin.student_course.edit',$studentCourse))->with(['error'=>'老师名字不正确']);
        }
        $input = $request->only('classes', 'schooltime','week','subject');
        $input['teachers_id']=$teacher->id;
        $studentCourse->update($input);
        return redirect(route('admin.student_course.index'))->with(['error'=>'修改修改']);
    }


    public function destroy(StudentCourse $studentCourse)
    {
        $studentCourse->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
}
